package cn.mifengkong.appcommon.customecamera;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Entity;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.net.Uri;
import android.util.Log;

import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created by WANG on 2018/6/29.
 */

public class NativeShareUtils {


    public static void shareMoreImgToWeChart( Context context , List<File> files){
        ArrayList<Uri> uris = new ArrayList<>();
        for (int i = 0; i < files.size(); i++) {
            File file = files.get(i);
            if(null == file){
                continue;
            }
            Log.e("WANG","NativeShareUtils.shareMoreImgToWeChart."+file );
            uris.add(Uri.fromFile(file));
        }

        Intent intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
        Log.e("WANG","NativeShareUtils.shareMoreImgToWeChart."+uris);
        ComponentName comp = new ComponentName("com.tencent.mm", "com.tencent.mm.ui.tools.ShareToTimeLineUI");
        intent.setComponent(comp);
        intent.setType("image/*");
        intent.putExtra(Intent.EXTRA_STREAM, uris);
        context.startActivity(Intent.createChooser(intent, "请选择"));

    }

    public static void start(final Context context, final List<String> images){
        String[] strings = images.toArray(new String[images.size()]);
        ImageUtils.getBitmapForUrlAndSaveToAlbum(context, new ImageUtils.LoanImgFinish() {
            @Override
            public void loanFinish(Map map) {
                ArrayList<File> files = new ArrayList<>();
                Log.e("WANG","NativeShareUtils.loanFinish.图片保存成功" +map);
                if(null != map && map.size() > 0){
                    Set set = map.entrySet();
                    Iterator iterator = set.iterator();
                    while (iterator.hasNext()){
                        Map.Entry entry = (Map.Entry) iterator.next();
                        File file = (File) entry.getValue();
                        files.add(file);
                    }
                    shareMoreImgToWeChart(context,files);

                }
            }
        }, strings);
    }

    private static boolean isInstallWeChat(Context context) {
        PackageInfo packageInfo = null;
        try {
            packageInfo = context.getPackageManager().getPackageInfo("com.tencent.mm", 0);
        } catch (Exception e) {
            packageInfo = null;
            e.printStackTrace();
        }
        if (packageInfo == null)
            return false;
        else
            return true;

    }


}
